/**
 * 
 */
package edu.rice.images;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;

import org.jfree.io.IOUtils;

import edu.rice.myxo.utils.IOutils;

import sun.tools.tree.UnsignedShiftRightExpression;

/**
 * @author frogy
 * 
 * Didn't finish, need more image file format to complete!
 * 
 */
public class ImageReader {

	int[][] imData;

	public ImageReader(String imageName) {
		Scanner sc = new Scanner(imageName).useDelimiter("\\.");
		String type = "";
		while (sc.hasNext()) {
			type = sc.next();
		}
		if (type.equals("jpg")) {
			readJPG(imageName);
		} else if (type.equals("tif")) {
			readTIF(imageName);
		} else if (type.equals("png")) {
			readPNG(imageName);
		} else {
			System.out.println("Unsupported image type");
		}
	}

	public void readJPG(String s) {
		System.out.println("Reading jpg file: " + s);
	}

	public void readTIF(String s) {
		System.out.println("Reading tif file: " + s);
		imData = new int[1024][1344];
		try {
			FileInputStream in = new FileInputStream(s);
			int c;
			int i = 0;
			int k = 0;
			int l = 0;
			while((c = in.read() )!= -1){
				if(i < 20){
					System.out.print(c + " ");
					if((i+1)%10==0){
						System.out.println();
					}
				}
				i++;
			}
			System.out.println("The file contains "+i+"bytes");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.err.println("File " + s + " doesn't exist!");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		IOutils iou = new IOutils();
		iou.writeMatrix(imData, "//Users/frogy/", "hello");
	}

	public void readPNG(String s) {
		System.out.println("Reading png file: " + s);
	}

	public static void main(String[] args) {
		new ImageReader("DIC000.tif");
	}
}
